package com.qingcheng.controller;

import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Album;
import com.qingcheng.pojo.goods.TbAlbum;
import com.qingcheng.service.AlbumService;
import com.qingcheng.service.TbAlbumService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    @Resource
    private TbAlbumService tbAlbumService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {

        TbAlbum tbAlbum = tbAlbumService.findById(id);
        return YouthR.ok().data("tbAlbum",tbAlbum);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbAlbum>  list = tbAlbumService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbAlbum> pageUtils = tbAlbumService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbAlbum tbAlbum){
        PageUtil<TbAlbum> pageUtils = tbAlbumService.pageQueryByAll(pageNum, pageSize, tbAlbum);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public YouthR add(@RequestBody TbAlbum tbAlbum) {

        boolean b = tbAlbumService.saveTbAlbum(tbAlbum);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbAlbum tbAlbum) {
        boolean b = tbAlbumService.updateTbAlbum(tbAlbum);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbAlbumService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Album> findAll(){
        return albumService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Album> findPage(int page, int size){
        return albumService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Album> findList(@RequestBody Map<String,Object> searchMap){
        return albumService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Album> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  albumService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Album findById(Long id){
        return albumService.findById(id);
    }


//    @PostMapping("/add")
//    public Result add(@RequestBody Album album){
//        albumService.add(album);
//        return new Result(200,"添加成功",null);
//    }

    @PostMapping("/update")
    public Result update(@RequestBody Album album){
        albumService.update(album);
        return new Result(200,"修改成功",null);
    }

    @GetMapping("/delete")
    public Result delete(Long id){
        albumService.delete(id);
        return new Result(200,"删除成功",null);
    }

}
